package com.keon.jianbao.ui.activity;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.ui.widget.DetailPicView;
import com.keon.jianbao.util.FileUtils;
import com.keon.jianbao.util.ImageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by keon on 15/10/18.
 */
public class PicDetailActivity extends BaseActivity {

    /*   @Bind(R.id.pic_detail)
       ZoomImageView ivDetail;*/
    @Bind(R.id.pic_viewpager)
    ViewPager vpPic;

    String uri;
    int position;
    ArrayList<String> uris;
    ArrayList<View> pageview;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pic_detail;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.pic_detail;
    }

    @Override
    public void initView() {
//        ivDetail.setOnLongClickListener(this);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        uris = bundle.getStringArrayList("uris");
        position = bundle.getInt("position");
//        uri = bundle.getString("uri");
//        ImageLoader.getInstance().displayImage(uri, ivDetail, UILUtils.getOptions2());

        initVierPager();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        return super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_pic_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.pic_detail_save:
                uri = uris.get(vpPic.getCurrentItem());
                String path = "/sdcard/jianbao/download/" + FileUtils.getFileName(uri);
                try {
                    ImageUtils.saveImageToSD(PicDetailActivity.this, path, ImageLoader.getInstance().loadImageSync(uri), 100);
                    PicDetailActivity.showToast("已保存到" + path);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
        }
        return true;
    }

    void initVierPager() {
        pageview = new ArrayList(uris.size());
        for (int i = 0; i < uris.size(); i++) {
            DetailPicView detailPicView = new DetailPicView(this, uris.get(i));
            pageview.add(detailPicView);
        }

        MyPagerAdapter mPagerAdapter = new MyPagerAdapter();
        //绑定适配器
        vpPic.setAdapter(mPagerAdapter);
        vpPic.setCurrentItem(position);
    }

    class MyPagerAdapter extends PagerAdapter {
        @Override
        //获取当前窗体界面数
        public int getCount() {
            // TODO Auto-generated method stub
            return pageview.size();
        }

        @Override
        //断是否由对象生成界面
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }
        //是从ViewGroup中移出当前View

        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageview.get(arg1));
        }

        //返回一个对象，这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(pageview.get(arg1));
            return pageview.get(arg1);
        }
    };
}